package com.wmx.main.text05.breeder;


import com.wmx.main.text05.Health;
import com.wmx.main.text05.Sleep;
import com.wmx.main.text05.animal.Animal;

import java.lang.reflect.Field;

/**
 * @author:wmx 2025-03-29 14:09
 * 功能:哺乳饲养员
 **/
 public  class LactationBreeder extends Breeder {


    public LactationBreeder(String name, String kind) {
        super(name, kind);
    }



    @Override
    public <T extends Animal> void feed(T t) {
        System.out.println("哺乳饲养员"+super.getName()+"喂养了"+t.getName());
        t.toString();
    }

    @Override
    public <T extends  Animal> void patrol(T t) {
        System.out.println("哺乳饲养员"+super.getName()+"巡查了"+t.getName());
        Class<?> getclass  = t.getClass();
        boolean annotationPresent = getclass.isAnnotationPresent(Sleep.class);
        if(!annotationPresent){
            try {
                throw new Exception("系统异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            try {

                //动物的睡眠状态
                Sleep annotation = getclass.getAnnotation(Sleep.class);
                String sleep = annotation.value();
                System.out.println(t.getName()+"睡眠状态:"+sleep);
                //动物吃安眠药状态
                Class<?> superclass = getclass.getSuperclass();//获取它的抽象类
                Field hypnotics = superclass.getDeclaredField("hypnotics");
                hypnotics.setAccessible(true);
                Object hypnoticsValue = hypnotics.get(t);//安眠药的数量


                //判断动物是否有吃安眠药
                if(sleep.equals("失眠") &&  (int)hypnoticsValue == 0){
                    //让动物吃安眠药
                    hypnotics.set(t,1);
                    System.out.println(t.getName()+"没有吃安眠药,喂了一颗");
                }

            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public <T extends  Animal> void examine(T t) {

        Class<?> getclass  = t.getClass();
        boolean annotationPresent = getclass.isAnnotationPresent(Health.class);
        if(!annotationPresent){
            try {
                throw new Exception("系统异常");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            Health annotation = getclass.getAnnotation(Health.class);
            System.out.println("哺乳饲养员"+super.getName()+"检查了"+t.getName()+"的身体状态:"+annotation.value());
        }

    }
}
